﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tutors_clus.ViewModel;
using tutors_clus.Models;
using PagedList;
using System.Text.RegularExpressions;
namespace tutors_clus.Controllers
{
    public class HomeController : Controller
    {
        tutors_clubContext db = new tutors_clubContext();
        public ActionResult Index()
        {
            var home_model = new Home_ViewModel();
            home_model.news = db.news.Where(s => s.status_new == true).Take(5).OrderBy(m => m.sort);//.OrderByDescending(y => y.create_date_new.Value.Year).ThenByDescending(m => m.create_date_new.Value.Month).ThenByDescending(d => d.create_date_new.Value.Day).ToList();
            home_model.annoucements = db.annoucements.Where(s => s.status_ann == true).OrderBy(m => m.sort);
            home_model.courses = db.courses.Where(s => s.status_cou == true && s.createdate_cou >= DateTime.Today).Take(5).OrderByDescending(y => y.createdate_cou.Value.Year).ThenByDescending(m => m.createdate_cou.Value.Month).ThenByDescending(d => d.createdate_cou.Value.Day).ToList();
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            return View(home_model);
        }
        public PartialViewResult list_courses()
        {
            var list = db.courses.Where(s => s.status_cou == true && s.createdate_cou >= DateTime.Today).Take(5).OrderByDescending(y => y.createdate_cou.Value.Year).ThenByDescending(m => m.createdate_cou.Value.Month).ThenByDescending(d => d.createdate_cou.Value.Day).ToList();
            return PartialView(list);
        }
        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        public ActionResult Annoucements(int? page)
        {
            int pageSize = 5;
            int pageNumber = (page ?? 1);
            var list = db.annoucements.Where(s=>s.status_ann==true).OrderByDescending(y=>y.create_date_ann.Value.Year).ThenByDescending(m=>m.create_date_ann.Value.Month).ThenByDescending(d=>d.create_date_ann.Value.Day).ToList();
            return View(list.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult Annoucement(int id)
        {
            var anno_detail = db.annoucements.Find(id);
            var list_other = db.annoucements.Where(i => i.id_ann != anno_detail.id_ann && i.create_date_ann < anno_detail.create_date_ann).OrderByDescending(y => y.create_date_ann.Value.Year).ThenByDescending(m => m.create_date_ann.Value.Month).ThenByDescending(d => d.create_date_ann.Value.Day);
            ViewBag.list = list_other;
            ViewBag.count = list_other.Count();
            if (anno_detail == null)
            {
                return HttpNotFound();
            }
            return View(anno_detail);
        }
        /*Hiển thị ds tin và phân trang*/
        public ActionResult News(int? page)
        {
            int pageSize = 5;
            int pageNumber = (page ?? 1);
            var list = db.news.Where(s => s.status_new == true).OrderByDescending(y => y.create_date_new.Value.Year).ThenByDescending(m => m.create_date_new.Value.Month).ThenByDescending(d => d.create_date_new.Value.Day).ToList();
            return View(list.ToPagedList(pageNumber, pageSize));
        }
        /*Tìm kiếm tin tức*/

        public PartialViewResult Search_News()
        {
            List<SelectListItem> select = new List<SelectListItem>();
            select.Add(new SelectListItem { Text = "Tìm theo tiêu đề", Value = "1", Selected = true });
            select.Add(new SelectListItem { Text = "Tìm theo ngày tháng", Value = "2" });
            ViewBag.search_select = select;
            return PartialView();
        }
        public PartialViewResult Search_Annou()
        {
            List<SelectListItem> select = new List<SelectListItem>();
            select.Add(new SelectListItem { Text = "Tìm theo tiêu đề", Value = "1", Selected = true });
            select.Add(new SelectListItem { Text = "Tìm theo ngày tháng", Value = "2" });
            ViewBag.search_select = select;
            return PartialView();
        }
        /*Kết quả tìm kiếm*/
        public ActionResult Search_News_Result(string searchString, string datepicker, int search_select)
        {
            if (search_select == 1)
            {
                if (!String.IsNullOrEmpty(searchString))
                {
                    string strSearch = ConvertToUnSign(searchString).ToLower();
                    List<news> listnews = (from c in db.news select c).ToList();
                    List<news> result = listnews.FindAll(
                            delegate(news math)
                            {

                                if (ConvertToUnSign(math.title_new.ToLower()).Contains(strSearch))

                                    return true;

                                else

                                    return false;

                            }
                            
                        );
                    //var searchbystring = db.news.Where(m => m.title_new.Contains(strSearch));
                    ViewBag.search = searchString;
                    ViewBag.count = result.Count();
                    return View(result);
                }
                else
                {
                    return HttpNotFound();
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(datepicker))
                {
                    DateTime a = Convert.ToDateTime(datepicker);
                    var searchbyday = db.news.Where(m => m.create_date_new == a);
                    ViewBag.search = "Ngày "+ datepicker;
                    ViewBag.count = searchbyday.Count();
                    return View(searchbyday);
                }
                else
                {
                    return HttpNotFound();
                }
            }
        }

        public ActionResult Search_Annou_Result(string searchString, string datepicker, int search_select)
        {
            if (search_select == 1)
            {
                if (!String.IsNullOrEmpty(searchString))
                {
                    string strSearch = ConvertToUnSign(searchString).ToLower();
                    List<annoucement> listnews = (from c in db.annoucements select c).ToList();
                    List<annoucement> result = listnews.FindAll(
                            delegate(annoucement math)
                            {

                                if (ConvertToUnSign(math.title_ann.ToLower()).Contains(strSearch))

                                    return true;

                                else

                                    return false;

                            }

                        );
                    //var searchbystring = db.news.Where(m => m.title_new.Contains(strSearch));
                    ViewBag.search = searchString;
                    ViewBag.count = result.Count();
                    return View(result);
                }
                else
                {
                    return HttpNotFound();
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(datepicker))
                {
                    DateTime a = Convert.ToDateTime(datepicker);
                    var searchbyday = db.annoucements.Where(m => m.create_date_ann == a);
                    ViewBag.search = "Ngày " + datepicker;
                    ViewBag.count = searchbyday.Count();
                    return View(searchbyday);
                }
                else
                {
                    return HttpNotFound();
                }
            }
        }
        /*Hiển thị chi tiết tin tức và load danh sách tin cũ hơn*/
        public ActionResult Detail_News(int id = 0)
        {
            var detail_new_id = db.news.Find(id);
            var list_other = db.news.Where(i => i.id_new != id && i.create_date_new <= detail_new_id.create_date_new).OrderByDescending(y => y.create_date_new.Value.Year).ThenByDescending(m => m.create_date_new.Value.Month).ThenByDescending(d => d.create_date_new.Value.Day);            
            ViewBag.list = list_other;
            ViewBag.count = list_other.Count();
            if (detail_new_id == null)
            {
                return HttpNotFound();
            }
            return View(detail_new_id);
        }
        /*Xem các lớp đang mở*/
        public ActionResult Coures(int ? page) 
        {
            int pageSize = 3;
            int pagaNumber = (page ?? 1);
            var list_coures = db.courses.Where(s => s.status_cou == true && s.createdate_cou >= DateTime.Today).OrderByDescending(y => y.createdate_cou.Value.Year).ThenByDescending(m => m.createdate_cou.Value.Month).ThenByDescending(d => d.createdate_cou.Value.Day).ToList();
            return View(list_coures.ToPagedList(pagaNumber,pageSize));
        }
        /*Hiển thị chi tiết lớp học*/
        public ActionResult Detail_Coures(int id = 0) 
        {
            var detail_couresid = db.courses.Find(id);
            var list = db.courses.Where(i => i.id_cou != detail_couresid.id_cou).Take(5);
            ViewBag.Count = list.Count();
            ViewBag.list = list;
            return View(detail_couresid);
        }
        public static string ConvertToUnSign(string text)
        {
            for (int i = 33; i < 48; i++)
            {
                text = text.Replace(((char)i).ToString(), "");
            }

            for (int i = 58; i < 65; i++)
            {
                text = text.Replace(((char)i).ToString(), "");
            }

            for (int i = 91; i < 97; i++)
            {
                text = text.Replace(((char)i).ToString(), "");
            }

            for (int i = 123; i < 127; i++)
            {
                text = text.Replace(((char)i).ToString(), "");
            }

            //text = text.Replace(" ", "-"); //Comment lại để không đưa khoảng trắng thành ký tự -

            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");

            string strFormD = text.Normalize(System.Text.NormalizationForm.FormD);

            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }

     }
    }
